package cn.leyou.service.impl;

import cn.leyou.mapper.UserMapper;
import cn.leyou.pojo.User;
import cn.leyou.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;

/**
 * @version 1.0.0
 * @Author yong.Mr
 * @data 2019-8-9
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired(required = false)
    private UserMapper userMapper;

    @Override
    public User queryById(Integer id) {
        //指定相应的实体类, 记住使用通用mapper, 相当于逆向工程, 通用mapper就是封装了逆向工程
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        //property 应该为实体类的属性名,不是@Column(name = "username")表的字段名字
        criteria.andEqualTo("id",id);
        criteria.andLike("userName", "%z%");
        User user = new User();
        user.setId(id);
        // 查询记录的条数
        int i = userMapper.selectCountByExample(example);
        User user1 = userMapper.selectByPrimaryKey(user);
        System.out.println("通用mapper的查询方法Service---->" + i);
        System.out.println("通用mapper的根据主键查询一条记录:----->" + user1.toString());
        return user;
    }
    @Override
    public User getTbUser(Integer id) {
        System.out.println("*****************测试insert返回主键id********************");
        User user = new User();
        double random = Math.random();
        user.setUserName(String.valueOf(random));
        user.setPassword("54546548484455");
        user.setPhone("545454");
        user.setCreated(new Date());
        user.setSalt("cscscs");
        int insertPrimary11 = userMapper.getInsertPrimary11(user);
        System.out.println("返回getInsertPrimary11影响的记录数------>:" + insertPrimary11);
        System.out.println("查看方法一getInsertPrimary11获得的主键通过user对象返回----->:" + user.getId());

        System.out.println("*************************************************");
        User user2 = new User();
        random = Math.random();
        user2.setUserName(String.valueOf(random));
        user2.setPassword("54546548484455");
        user2.setPhone("545454");
        user2.setCreated(new Date());
        user2.setSalt("cscscs");
        int insertPrimary22 = userMapper.getInsertPrimary22(user2);
        System.out.println("返回getInsertPrimary22影响的记录数------>:" + insertPrimary22);
        System.out.println("查看方法二getInsertPrimary22获得的主键通过user对象返回----->:" + user2.getId());

        System.out.println("*************************************************");
        User user3 = new User();
        random = Math.random();
        user3.setUserName(String.valueOf(random));
        user3.setPassword("54546548484455");
        user3.setPhone("545454");
        user3.setCreated(new Date());
        user3.setSalt("cscscs");
        int insertPrimaryNote33 = userMapper.getInsertPrimaryNote33(user3);
        System.out.println("返回insertPrimaryNote33影响的记录数------>:" + insertPrimaryNote33);
        System.out.println("查看方法三insertPrimaryNote33获得的主键通过user对象返回----->:" + user3.getId());

        System.out.println("**********************通用Mapper中的insert返回自动递增的主键(强烈推荐使用)***************************");
        User user4 = new User();
        random = Math.random();
        user4.setUserName(String.valueOf(random));
        user4.setPassword("54546548484455");
        user4.setPhone("545454");
        user4.setCreated(new Date());
        user4.setSalt("cscscs");
        int insertMapper = userMapper.insert(user4);
        System.out.println("返回通用Mapper中的insert影响的记录数------>:" + insertMapper);
        System.out.println("查看方法四通用Mapper中的insert获得的主键通过user对象返回----->:" + user4.getId());

        // 通用Mapper中insert和insertSelective都可以返回主键自动递增的主键,直接映射到传入参数(user5)的对象中属性上 ,直接在传入参数对象中获取自动递增的主键

        System.out.println("*******************通用Mapper中的insertSelective******************************");
        User user5 = new User();
        random = Math.random();
        user5.setUserName(String.valueOf(random));
        user5.setPassword("54546548484455");
        user5.setPhone("545454");
        user5.setCreated(new Date());
        user5.setSalt("cscscs");
        int insertMapper11 = userMapper.insertSelective(user5);
        System.out.println("返回通用Mapper中的insertSelective影响的记录数------>:" + insertMapper11);
        System.out.println("查看方法五通用Mapper中的insertSelective获得的主键通过user对象返回----->:" + user5.getId());
        System.out.println("*************************************************");

        System.out.println("*******************通用Mapper中相关update方法******************************");


        return userMapper.getTbUser(id);
    }

}
